﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Pyramid.Bll;
using Pyramid.Models;

namespace Pyramid.Controllers
{
	/// <summary>
	/// 员工业绩
	/// </summary>
	public class AchievementController : BaseController
	{
		private readonly PaymentHelper _helper = new PaymentHelper();

		public ActionResult List(string keywords, int? year, int? month)
		{
			var model = new StaffMonthlyQueryModel {Keywords = keywords};

			var date = DateTime.Now.AddMonths(-1);
			model.Year = year ?? date.Year;
			model.Month = month ?? date.Month;

			if (model.Year >= date.Year && model.Month > date.Month)
			{
				return View("_Error", new ErrorViewModel {Message = string.Format("选择的月份不能晚于{0:yyyy年M月}，请重新选择！", date)});
			}

			ViewBag.QueryModel = model;
			var master = _helper.GetMaster(model.Year, model.Month);
			ViewBag.Master = master;

			return master == null ? View(new List<StaffAchievementViewModel>()) : View(_helper.GetStaffAchievementList(model));
		}

		/// <summary>
		/// 业绩统计
		/// </summary>
		public ActionResult Gather(int year, int month)
		{
			return !_helper.GatherStaffAchievement(year, month, User) ? JError(_helper.Message) : JSuccess("统计成功！", "tab_sal_achievement");
		}

		/// <summary>
		/// 标记为已结算
		/// </summary>
		public ActionResult Settle(int id)
		{
			return !_helper.Settle(id) ? JError(_helper.Message) : JSuccess("结算成功！", "tab_sal_achievement");
		}

		/// <summary>
		/// 用户的业绩详情
		/// </summary>
		public ActionResult Details(string userId, int year, int month)
		{
			return View(_helper.GetDetails4StaffAchievement(userId, year, month));
		}

		/// <summary>
		/// 我的业绩查询
		/// </summary>
		public ActionResult MyList(int? year, int? month)
		{
			if (User.UserType == UserTypes.Customer)
			{
				return NoAuthorization("此功能不对顾客开放！");
			}

			var date = DateTime.Now.AddMonths(-1);
			var y = year ?? date.Year;
			var m = month ?? date.Month;
			ViewBag.Year = y;
			ViewBag.Month = m;
			return View(_helper.GetDetails4StaffAchievement(User.UserId, y, m));
		}

		/// <summary>
		/// 管理中心业绩查询
		/// </summary>
		public ActionResult AdminCenterList(int? year, int? month)
		{
			if (User.UserType != UserTypes.AdminCenter)
			{
				return NoAuthorization("此功能仅对管理中心用户开放！");
			}

			var date = DateTime.Now.AddMonths(-1);
			var y = year ?? date.Year;
			var m = month ?? date.Month;
			ViewBag.Year = y;
			ViewBag.Month = m;
			return View(_helper.GetCenterAchievement((int)UserTypes.AdminCenter, User.ExtInfoId, y, m));
		}

		public ActionResult MemberCenterList(int? year, int? month)
		{
			if (User.UserType != UserTypes.MemberCenter)
			{
				return NoAuthorization("此功能仅对会员中心用户开放！");
			}

			var date = DateTime.Now.AddMonths(-1);
			var y = year ?? date.Year;
			var m = month ?? date.Month;
			ViewBag.Year = y;
			ViewBag.Month = m;
			return View(_helper.GetCenterAchievement((int)UserTypes.MemberCenter, User.ExtInfoId, y, m));
		}
	}
}